package com.polaris.live.adapter.home

import android.graphics.Color
import android.graphics.Typeface
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.resp.back_resp.BannerPromptResp
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * BannerPromptAdapter
 *
 * @author Created by lucas on 2024/6/26 02:30
 * @since 1.0.0
 */
class BannerPromptAdapter : BaseBannerAdapter<BannerPromptResp>() {

    /**
     * 获取数据
     */
    fun getList(): List<BannerPromptResp> {
        return mList
    }

    override fun bindData(
        holder: BaseViewHolder<BannerPromptResp>,
        data: BannerPromptResp,
        position: Int,
        pageSize: Int,
    ) {
        val bgLayout = holder.findViewById<View>(R.id.fl_layout)
        val iconView = holder.findViewById<ImageView>(R.id.iv_icon)
        val textView = holder.findViewById<TextView>(R.id.tv_text)

        if (data.icon.isNullOrEmpty()) {
            iconView.visibility = View.GONE
        } else {
            iconView.visibility = View.VISIBLE
            Glide.with(iconView)
                .load(ImageUtils.imgPath(data.icon))
                .into(iconView)
        }

        RouterUtils.handleRouterText(
            textView,
            data.content,
            data.highlight?.map { it.toRouter() } ?: emptyList(),
            emptyMap()
        )

        val textColor = data.color?.let { Color.parseColor(it) }
            ?: ColorUtils.getColor(color.color_fa3a9e)
        textView.setTextColor(textColor)

        val textBold = data.bold ?: false
        val textItalic = data.italic ?: false
        when {
            textBold && textItalic -> {
                textView.setTypeface(null, Typeface.BOLD_ITALIC)
            }

            textBold -> {
                textView.setTypeface(null, Typeface.BOLD)
            }

            textItalic -> {
                textView.setTypeface(null, Typeface.ITALIC)
            }

            else -> {
                textView.setTypeface(null, Typeface.NORMAL)
            }
        }

        val bgColor = data.bgColor?.let { Color.parseColor(it) }
            ?: ColorUtils.getColor(R.color.color_1afa3a9e)
        bgLayout.setBackgroundColor(bgColor)
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_prompt_abs
    }
}